package com.apes.fn.scm.crossCompany.transfer.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 *
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = BatchDump.CONST_TABLE_NAME)
public class BatchDump extends Model {
    public static final String CONST_TABLE_NAME = "stock_batch_dump";
    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "批量转储", prefix = "PH%(company)%(yy)%(month)", padding = 5, useDateRange=true)
    private String id;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "ship_location_id", referencedColumnName = "id")
    @Comment("发货仓库")
    private Location shipLocation;

    @ManyToOne
    @JoinColumn(name = "move_type", referencedColumnName = "id")
    @Comment("原因")
    private Enumeration returnReason;

    @Column(name = "state", length = 10, nullable = false)
    @States(name = "订单状态", values = {
            @State(id = "sent", name = "新建"),
//            @State(id = "apply", name = "提交"),
            @State(id = "approve", name = "审核"),
            @State(id = "delete", name = "删除")})
    private String state;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @Column(name = "print_number", columnDefinition = "number(4,0) default 0")
    @Comment("打印次数")
    private double printNumber;

    @ManyToOne
    @JoinColumn(name = "create_uid", referencedColumnName = "id")
    @Comment("创建人")
    private PartyRole createUid;

    @ManyToOne
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid;

    @Column(name = "date_approve")
    @Comment("审批日期")
    private Date dateApprove;

    @ManyToOne
    @JoinColumn(name = "delet_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid;

    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate;

    @OrderBy("receiptLocation.id asc")
    @OneToMany(mappedBy = "batchDump", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<BatchDumpItem> batchDumpItems = new ArrayList<>();

    public List<BatchDumpItem> getBatchDumpItems() {
        for (BatchDumpItem item : batchDumpItems) {
            item.setBatchDump(this);
        }
        return batchDumpItems;
    }

}
